package aes.server;

import javax.swing.JOptionPane;

import aes.general.Msg;
import ocsf.server.*;

/**
 * This class overrides some of the methods in the abstract superclass in order
 * to give more functionality to the server.
 * 
 */

public class AesServer extends AbstractServer {
	// Class variables *************************************************

	/**
	 * The default port to listen on.
	 */
	final public static int DEFAULT_PORT = 5555;

	// Constructors ****************************************************

	/**
	 * Constructs an instance of the echo server.
	 * 
	 * @param port
	 *            The port number to connect on.
	 */
	public AesServer(int port) {
		super(port);
	}

	// Instance methods ************************************************

	/**
	 * This method handles any messages received from the client.
	 * 
	 * @param msg
	 *            The message received from the client.
	 * @param client
	 *            The connection from which the message originated.
	 */
	public void clientConnected(ConnectionToClient client) {
		System.out.println(client.getName());
	}

	/* (non-Javadoc)
	 * @see ocsf.server.AbstractServer#handleMessageFromClient(java.lang.Object, ocsf.server.ConnectionToClient)
	 */
	public void handleMessageFromClient(Object msg, ConnectionToClient client) {

		// System.out.println(" Message received: " +msg.toString() +" from " +
		// client);
		System.out.println("recieved");

		/**
		 * Method handles the first and former interaction with the system 
		 * throught the login query.
		 */
		if (((Msg) msg).getRequestType().equals("100"))

			ServerLogic.RunLoginQuery(msg, client);

		/**
		 * Methods handles messages received from Teacher (client)
		 */
		else if (((Msg) msg).getRequestType().equals("400"))

			ServerLogic.RunAddQuestionQuery(msg, client);

		else if (((Msg) msg).getRequestType().equals("401"))

			ServerLogic.getCourseQuery(msg, client);

		else if (((Msg) msg).getRequestType().equals("402"))

			ServerLogic.getQuestionsQuery(msg, client);
		
		else if (((Msg) msg).getRequestType().equals("403"))

			ServerLogic.InsertExamQuery(msg, client);
		
		else if(((Msg) msg).getRequestType().equals("404"))
			
			ServerLogic.getActiveExamQuery(msg, client);
		
		else if(((Msg) msg).getRequestType().equals("405"))
			
			ServerLogic.setActivationCodeQuery(msg, client);
		
		else if(((Msg) msg).getRequestType().equals("406")) 
			
			ServerLogic.getStatExamQuery(msg, client);
		
		
		else if(((Msg) msg).getRequestType().equals("408")) 
			
			ServerLogic.runGetActiveExamsQuery(msg, client);
		
		else if(((Msg) msg).getRequestType().equals("409")) 
			
			ServerLogic.runSetRequestQuery(msg, client);
		
		 else if(((Msg) msg).getRequestType().equals("410")) 
			   
		   ServerLogic.runGetNotApprovedExamQuery(msg, client);
			  
		 else if(((Msg) msg).getRequestType().equals("411")) 
	   
			 ServerLogic.runGetNotApprovedInfoQuery(msg, client);
    
        else if(((Msg) msg).getRequestType().equals("413")) 
            
            ServerLogic.runGetQidQuery(msg, client);
		
        else if(((Msg) msg).getRequestType().equals("414")) 
            
            ServerLogic.runInsertApproveQidQuery(msg, client);
		
        else if(((Msg) msg).getRequestType().equals("415")) 
            
            ServerLogic.runChangeGradeQuery(msg, client);
	
        else if(((Msg) msg).getRequestType().equals("416")) 
            
            ServerLogic.runLockExamQuery(msg, client);
        
        else if(((Msg) msg).getRequestType().equals("417")) 
       
            ServerLogic.RunGetExamReport(msg, client);
		
        else if(((Msg) msg).getRequestType().equals("418")) 
            
            ServerLogic.RunGetExamInfo(msg, client);
      
        

		/**
		 * Methods handles messages received from Student (client)
		 */

		else if (((Msg) msg).getRequestType().equals("202"))

			ServerLogic.RunStudentGetExamQuery(msg, client);

		else if (((Msg) msg).getRequestType().equals("203"))

			ServerLogic.RunDownloadExamQuery(msg, client);
		
		else if (((Msg) msg).getRequestType().equals("204"))
			
			ServerLogic.RunSaveManualExamFile(msg, client);
		
		else if (((Msg) msg).getRequestType().equals("205"))
			
			ServerLogic.RunGetGradesExamQuery(msg, client);
		
		else if (((Msg) msg).getRequestType().equals("206"))
			
			ServerLogic.RunDeleteActiveCodeQuery(msg, client);
	
		else if (((Msg) msg).getRequestType().equals("207"))
			
			ServerLogic.RunDownloadExamAutomaticQuery(msg, client);
		
		else if (((Msg) msg).getRequestType().equals("208"))
			
			ServerLogic.RunCompletedAutomaticExamQuery(msg, client);
		

		else if (((Msg) msg).getRequestType().equals("210"))
			
			ServerLogic.RungetFininsedExamInfo(msg, client);
		

		else if (((Msg) msg).getRequestType().equals("209"))
			
			ServerLogic.RunGetmanualExamslistQuery(msg, client);
		

		else if (((Msg) msg).getRequestType().equals("211"))
			
			ServerLogic.RundownoadManualExamCopytQuery(msg, client);
		

		/**
		 * Methods handles messages received from Admin (client)
		 */
		else if(((Msg) msg).getRequestType().equals("300"))
			
			ServerLogic.RunGetRequestsQuery(msg, client);
		
		else if(((Msg) msg).getRequestType().equals("301"))
			
			ServerLogic.RunAdminFillTeacherComboBoxQuery(msg, client);
		
		else if(((Msg) msg).getRequestType().equals("303")) //request to get examId of the teacher
			  ServerLogic.RunAdminGetExamIdQuery(msg, client);
			
			/*ServerLogic.RunAdminGetStatTeacherQuery(msg, client);*/
		
		else if(((Msg) msg).getRequestType().equals("304"))
			
			ServerLogic.RunAdminGetExamsQuery(msg, client);
		
		else if(((Msg) msg).getRequestType().equals("305"))
			
			ServerLogic.RunAdminGetQuestionsQuery(msg, client);
		
		else if(((Msg) msg).getRequestType().equals("306"))
			
			ServerLogic.RunAddExamTimeFunction(msg,client);
		
		else if(((Msg) msg).getRequestType().equals("307"))
			
			ServerLogic.RungetCoursesQuery(msg,client);
		
		else if(((Msg) msg).getRequestType().equals("312"))
			
			ServerLogic.RunGetExamReport(msg,client);
		
		else if(((Msg) msg).getRequestType().equals("320"))
			
			ServerLogic.RunStudentInfoQuery(msg,client);
		
		else if(((Msg) msg).getRequestType().equals("321"))
			
			ServerLogic.RunStudentStatQuery(msg,client);
		
		else if(((Msg) msg).getRequestType().equals("322"))
			
			ServerLogic.RunTeacherInfoQuery(msg,client);
		
		else if(((Msg) msg).getRequestType().equals("323"))
			
			ServerLogic.RunTeacherExamInfoQuery(msg,client);

		else if(((Msg) msg).getRequestType().equals("324"))
			
			ServerLogic.RunTeacherExamGradesQuery(msg,client);
		
		else if(((Msg) msg).getRequestType().equals("325"))
			
			ServerLogic.RunCourseInfoQuery(msg,client);
		
	else if(((Msg) msg).getRequestType().equals("326"))
			
			ServerLogic.RunCourseExamInfoQuery(msg,client);
		
			
	}

	/**
	 * This method overrides the one in the superclass. Called when the server
	 * starts listening for connections.
	 */
	protected void serverStarted() {
		System.out.println("Server listening for connections on port "
				+ getPort());
		JOptionPane.showMessageDialog(null, "Server Started", "",
				JOptionPane.INFORMATION_MESSAGE);
	}

	/**
	 * This method overrides the one in the superclass. Called when the server
	 * stops listening for connections.
	 */
	protected void serverStopped() {
		System.out.println("Server has stopped listening for connections.");
	}

	// Class methods ***************************************************

	/**
	 * This method is responsible for the creation of the server instance (there
	 * is no UI in this phase).
	 * 
	 * @param args
	 *            [0] The port number to listen on. Defaults to 5555 if no
	 *            argument is entered.
	 */
	public static void startNewServer(String svport) {

		int port = 0; // Port to listen on

		try {
			port = Integer.parseInt(svport); // Get port from command line
		} catch (Throwable t) {

			port = DEFAULT_PORT; // Set port to 5555
			JOptionPane.showMessageDialog(null,
					"Port Error,Using Default Port :5555", "Error",
					JOptionPane.ERROR_MESSAGE);
		}

		AesServer sv = new AesServer(port);
		ServerLogic.server = sv;

		try {
			sv.listen(); // Start listening for connections
		} catch (Exception ex) {
			System.out.println("ERROR - Could not listen for clients!");
			JOptionPane.showMessageDialog(null, "Server Not Started", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}
}
// End of EchoServer class
